package tcc.sglj.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

//import javax.servlet.http.HttpServletResponse;
//import org.apache.struts2.ServletActionContext;

import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.ProjectionList;



import tcc.sglj.util.HibernateUtil;
import tcc.sglj.model.User;

/**
 *
 * @author Jhonatan L S Santos
 */
@Namespace(value = "/list")
@ParentPackage(value = "json-default")
public class GridUserAction {

    private int total;
    private int page;
    private int records;
    private int rows;
    private String sidx;
    private String sord;
    private List data;
    private int start;
    private String field;
    private String search;

    @Action(value = "/user", results =
    @Result(name = "success", type = "json", params = {"excludeProperties",
        "message, order, sidx, sord, rows"}))
    public String listUser() {

        try {
            Session session = HibernateUtil.getSession();

            this.setRecords(Integer.parseInt(session
                    .createCriteria(User.class)
                    .add(Restrictions.eq("status", true))
                    .setProjection(Projections.rowCount())
                    .list().iterator().next().toString()));

            this.setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

            setStart(getRows() * getPage() - getRows());

            Criteria crit = session.createCriteria(User.class);
            ProjectionList projection = Projections.projectionList();
            projection.add(Projections.property("id"));
            projection.add(Projections.property("name"));
            projection.add(Projections.property("email"));
            projection.add(Projections.property("cpf"));
            projection.add(Projections.property("sign"));
            projection.add(Projections.property("phone"));
            projection.add(Projections.property("permission"));
            crit.add(Restrictions.eq("status", true));

            crit.setProjection(projection);

            if (getSord().equals("desc")) {
                crit.addOrder(Order.desc(getSidx()));
            } else {
                crit.addOrder(Order.asc(getSidx()));
            }

            if (getField() != null && getSearch() != null) {
                    crit.add(Restrictions.ilike(getField(), "%" + getSearch() + "%"));                
            }

            crit.setFirstResult(getStart());
            crit.setMaxResults(getRows());

            this.setData(crit.list());
            
             if (getField() != null && getSearch() != null) {
                this.setRecords(this.getData().size());
            }

        } catch (Exception e) {
            e.getCause().toString();
        }
        return "success";
    }

    /**
     * @return the total
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * @return the page
     */
    public int getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * @return the records
     */
    public int getRecords() {
        return records;
    }

    /**
     * @param records the records to set
     */
    public void setRecords(int records) {
        this.records = records;
    }

    /**
     * @return the sidx
     */
    public String getSidx() {
        return sidx;
    }

    /**
     * @param sidx the sidx to set
     */
    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    /**
     * @return the sord
     */
    public String getSord() {
        return sord;
    }

    /**
     * @param sord the sord to set
     */
    public void setSord(String sord) {
        this.sord = sord;
    }

    /**
     * @return the start
     */
    public int getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(int start) {
        this.start = start;
    }

    /**
     * @return the rows
     */
    public int getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * @return the data
     */
    public List getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List data) {
        this.data = data;
    }

    /**
     * @return the field
     */
    public String getField() {
        return field;
    }

    /**
     * @param field the field to set
     */
    public void setField(String field) {
        this.field = field;
    }

    /**
     * @return the search
     */
    public String getSearch() {
        return search;
    }

    /**
     * @param search the search to set
     */
    public void setSearch(String search) {
        this.search = search;
    }
}
